package com.xwaf.platform.web.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.xwaf.platform.common.utils.report.JasperreportsUtils;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.response.TestReportResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@RestController
@RequestMapping("/x/report")
@Api(hidden = true)
public class ReportController extends BaseController{

	@ApiOperation("显示报表")
	@GetMapping(value = "showReport", produces = "application/json;charset=UTF-8")
	public void showReport(HttpServletResponse response) {
		try {
			//1 准备数据
			List<TestReportResponse> list = Lists.newArrayList();
			TestReportResponse sr = null;
			for (int i=0; i<57; i++) {
				sr = new TestReportResponse();
				sr.setAccessPlatformName("PLATFOM");
				sr.setStationName("長沙"+String.valueOf(new Random().nextInt(100)));
				sr.setLmntOrderNum(new Random().nextInt(100));
				sr.setLmntTicketNum(new Random().nextInt(100));
				sr.setLmntTicketFee(Float.valueOf(new Random().nextInt(100)));
				sr.setCmOrderNum(new Random().nextInt(100));
				sr.setCmTicketNum(new Random().nextInt(100));
				sr.setCmTicketFee(Float.valueOf(new Random().nextInt(100)));
				sr.setCmRefundOrderNum(new Random().nextInt(100));
				sr.setCmRefundTicketNum(new Random().nextInt(100));
				sr.setCmRefundTicketFee(Float.valueOf(new Random().nextInt(100)));
				sr.setCmRefundTicketChargeFee(Float.valueOf(new Random().nextInt(100)));
				sr.setCmtOrderNum(new Random().nextInt(100));
				sr.setCmtTicketNum(new Random().nextInt(100));
				sr.setCmtTicketFee(Float.valueOf(new Random().nextInt(100)));
				sr.setCmntOrderNum(new Random().nextInt(100));
				sr.setCmntTicketNum(new Random().nextInt(100));
				sr.setCmntTicketFee(Float.valueOf(new Random().nextInt(100)));
				sr.setCmSettlementFee(Float.valueOf(new Random().nextInt(100)));
				list.add(sr);
			}
			
			//jasper文件
			String jasperPath = ResourceUtils.getFile("classpath:jasper/onlineSalesSettlement.jasper").getPath();
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("SETTLEMENT_MONTH","2019-05");
			params.put("COMPANY","心為愛放");
			params.put("CREATOR","李c");
			JasperreportsUtils util = new JasperreportsUtils(request, response);
			JasperPrint jasperPrint = util.getJasperPrintWithBeanList(jasperPath, params, list);
			
			//以分页的形式输出html
			util.createHtmlByPage(jasperPrint, "2");
			
			//util.createExportDocument(DocType.XLS, jasperPath, params, list, "Export");
			
            // 导出PDF begin
			 /*response.setContentType("application/pdf");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=test.pdf");
            
            JRPdfExporter exporter = new JRPdfExporter(); 
            JRStyle style = new JRBaseStyle();
            style.setPdfEmbedded(true);
            style.setPdfFontName("STSong-Light");
            style.setPdfEncoding("UniGB-UCS2-H");
            jasperPrint.setDefaultStyle(style);
            //设置exporter的参数  
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());  
            exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); 
            //执行exporter  
            exporter.exportReport();*/  
            // 导出PDF end
            
			//导出Excel begin
			//设置导出时参数  
			//以附件形式保存文件
            /*response.setContentType("application/vnd.ms-excel");  
            response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("互联网售票结算报表.xlsx","utf8"));  
            SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();  
            conf.setWhitePageBackground(false);  
            conf.setRemoveEmptySpaceBetweenRows(true);
            conf.setRemoveEmptySpaceBetweenColumns(true);
            conf.setDetectCellType(true);  

            JRXlsxExporter exporter = new JRXlsxExporter();  
            exporter.setConfiguration(conf);  

            //设置输入项  
            ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);  
            exporter.setExporterInput(exporterInput);  

            //设置输出项  
            OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(response.getOutputStream());  
            exporter.setExporterOutput(exporterOutput);  
            exporter.exportReport();*/
            //导出Excel end
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JRException e) {
			e.printStackTrace();
		}
	}
}
